package hibernate;

import modelo.Departamento;
import modelo.Empleado;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * hibernate
 * @author DiegoAlejandro
 *
 */
public class HibernateUtil {
	private static SessionFactory sessionFactory = null;
	
	/**
	 * Levanta la configuración y obtiene el session factory.
	 * @return sessionFactory
	 */
    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            Configuration configuration = 	new Configuration().configure()
            								.addAnnotatedClass(Departamento.class)
            								.addAnnotatedClass(Empleado.class);
            
            ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
            registry.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
             
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);           
        }
         
        return sessionFactory;
    }
    
    /**
     * cierra el session factory
     */
    public static void shutdown(){
    	getSessionFactory().close();
    }
}